{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests ##导入requests\n",
    "from bs4 import BeautifulSoup ##导入bs4中的BeautifulSoup\n",
    "import xlwt\n",
    "import os\n",
    "import time##限制访问频率\n",
    "\n",
    "headers = {'User-Agent':\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1\"}##浏览器请求头（大部分网站没有这个请求头会报错、请务必加上哦）\n",
    "\n",
    "#获取入口页面\n",
    "all_url = 'http://www.cae.cn/cae/html/main/col53/column_53_xb6.html'\n",
    "first_url = 'http://www.cae.cn' #拼接地址\n",
    "\n",
    "num = 0\n",
    "\n",
    "#获取页面\n",
    "start_html = requests.get(all_url, headers=headers)  ##使用requests中的get方法来进行请求\n",
    "\n",
    "#建立空数组\n",
    "co_url = []\n",
    "nam_li = []\n",
    "\n",
    "#beautifulsoup解析网页\n",
    "Soup = BeautifulSoup(start_html.text, 'lxml') ##使用BeautifulSoup来解析我们获取到的网页（‘lxml’是指定的解析器 具体请参考官方文档哦）\n",
    "\n",
    " ##使用BeautifulSoup解析网页过后就可以用找标签呐！（find_all是查找指定网页内的所有标签的意思，find_all返回的是一个列表。）\n",
    "\n",
    "#获取所有院士名字\n",
    "name_list = Soup.find('div',class_='right_md_ysmd').find_all('a')\n",
    "\n",
    "#print(name_list)\n",
    "for name in name_list:\n",
    "    nam_li.append(name.get_text())\n",
    "    \n",
    "#print(nam_li)\n",
    "#获取院士详细内容页\n",
    "url_list = Soup.find('div',class_='right_md_ysmd').find_all('a')\n",
    "for url in url_list:\n",
    "    co_url.append(first_url+url['href'])\n",
    "#print(a)\n",
    "\n",
    "#url_list = Soup.find('')\n",
    "#print(url_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all done\n"
     ]
    }
   ],
   "source": [
    "#数据写入\n",
    "xls = xlwt.Workbook() \n",
    "sht1 = xls.add_sheet('Sheet1') \n",
    "\n",
    "for a in co_url:\n",
    "\n",
    "#获取院士详细内容\n",
    "    contx_html = requests.get(a, headers=headers)\n",
    "    res_Soup = BeautifulSoup(contx_html.text, 'lxml') ##使用BeautifulSoup来解析我们获取到的网页（‘lxml’是指定的解析器 具体请参考官方文档哦）\n",
    "    context_detail = res_Soup.find('div',class_='intro').find_all('p')\n",
    "#    print(str(context_detail),type(str(context_detail)))\n",
    "    sht1.write(num,0,str(context_detail))\n",
    "    num = num+1\n",
    "    sht1.write(num-1,1,str(nam_li[num-1]))\n",
    "    \n",
    "\n",
    "xls.save('./mydata.xls')\n",
    "print('all done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
